﻿using System;
using System.Configuration;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MagicCardManager
{
    public partial class AddCard : Form
    {
        private SqLiteHandle sqlHndl = null;
        private DataSet inSet = null;
        private DataSet outSet = null;
        private string source = null;

        private dsDb ds = null;

        public AddCard()
        {
            InitializeComponent();
        }

        private void Form_AddCard_Load(object sender, EventArgs e)
        {
            ds = null;
            ds = new dsDb();

            txtbox_Name.Text = "";
            checkedListBox_Edition.Items.Clear();
            comboBox_Rarity.Items.Clear();
            checkedListBox_ColorList.Items.Clear();
            checkedListBox_TypMain.Items.Clear();
            textBox_Artist.Text = "";
            txtbox_Anzahl.Text = "";
            txtbox_Amount.Text = "";

            source = ConfigurationManager.AppSettings.Get("database");

            try
            {
                sqlHndl = new SqLiteHandle(source);

                ds = sqlHndl.GetColorList();
                foreach (DataRow row in ds.Tables["color_list"].Rows)
                {
                    if (row["color"].ToString() == "NULL")
                        checkedListBox_ColorList.Items.Add("alle");
                    else
                        checkedListBox_ColorList.Items.Add(row["color"]);
                }

                ds = sqlHndl.GetTypMainList();
                foreach (DataRow row in ds.Tables["typ_main_list"].Rows)
                {
                    if (row["typ_main"].ToString() == "NULL")
                        continue;
                    else
                        checkedListBox_TypMain.Items.Add(row["typ_main"]);
                }

                ds = sqlHndl.GetRarityList();
                foreach (DataRow row in ds.Tables["rarity_list"].Rows)
                {
                    if (row["rarity"].ToString() == "NULL")
                        comboBox_Rarity.Items.Add("");
                    else
                        comboBox_Rarity.Items.Add(row["rarity"]);
                }



                ds = sqlHndl.GetEditionListList();
                foreach (DataRow row in ds.Tables["edition_list"].Rows)
                {
                    if (row["edition"].ToString() == "NULL")
                        checkedListBox_Edition.Items.Add("alle");
                    else
                        checkedListBox_Edition.Items.Add(row["edition"]);
                }
            }
            catch (Exception ex)
            {
                int jj = 0;
            }

        }

        private void btn_Add_Click(object sender, EventArgs e)
        {
            //dsDb ds;
            //ds = new dsDb();



            DataRow row = ds.Tables["card"].NewRow();

            row["name"] = txtbox_Name.Text;
            row["artist"] = textBox_Artist.Text;

            try
            {
                row["anzahl"] = Convert.ToInt32(txtbox_Anzahl.Text);
            }
            catch (Exception anz)
            {
                row["anzahl"] = 0;
            }

            try
            {
                row["amount"] = Convert.ToInt32(txtbox_Amount.Text);
            }
            catch (Exception anz)
            {
                row["amount"] = 0.0;
            }

            //for (int i = 0; i < checkedListBox_TypMain.Items.Count; i++)
            row["typ_main_list_id"] = 1;
            foreach (int indexChecked in checkedListBox_Edition.CheckedIndices)
            {

                row["typ_main_list_id"] = indexChecked + 1;

            }
            if (comboBox_Rarity.SelectedIndex < 1)
                row["rarity_list_id"] = 1;
            else
                row["rarity_list_id"] = comboBox_Rarity.SelectedIndex + 1;



            ds.Tables["card"].Rows.Add(row);


  
            row = ds.Tables["edition"].NewRow();
            //try
            //{
            //    row["anzKarten"] = Convert.ToInt32(txtbox_Anzahl.Text);
            //}
            //catch (Exception anz)
            //{
            //    row["anzKarten"] = 0;
            //}

           
            for (int i = 0; i < checkedListBox_Edition.Items.Count; i++)
                //foreach (int indexChecked in checkedListBox_Edition.CheckedItems.)
            {
                if (checkedListBox_Edition.GetItemChecked(i) == false) continue;
                  
                try
                {

                    row = ds.Tables["edition"].NewRow();
                    row["edition_id"] = i + 1;
                    row["card_id"] = 1;
                    row["edition_list_id"] = i + 1;
                    ds.Tables["edition"].Rows.Add(row);
                }
                catch (Exception hj)
                {
                    int jkk = 2;
                }
            }
            row = ds.Tables["color"].NewRow();
            for (int i = 0; i < checkedListBox_ColorList.Items.Count; i++)
            //foreach (int indexChecked in checkedListBox_Edition.CheckedIndices)
            {
                if (checkedListBox_ColorList.GetItemChecked(i) == false) continue;

                row = ds.Tables["color"].NewRow();
                row["color_list_id"] = i + 1;
                row["card_id"] = i + 1;
                row["color_id"] = i + 1;
                ds.Tables["color"].Rows.Add(row);
            }

            try
            {
                sqlHndl = null;
                sqlHndl = new SqLiteHandle(source);
                sqlHndl.AddCard(ds);
            }
            catch (Exception ex)
            {
                int hjh = 0;
            }



        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            //this.Close();

        }

        private void checkedListBox_Edition_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            int h;
            if (checkedListBox_Edition.GetItemChecked(e.Index).ToString() == "alle")
                h = 0;
        }

        private void checkedListBox_ColorList_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            int h;
            if (e.Index == 0)
                //if (checkedListBox_ColorList.GetItemChecked(e.Index))
                //    foreach (int row in checkedListBox_ColorList.Items)

                h = 0;
        }

    }
}
